#!/bin/bash

INSTALL_DIR="/usr/local/bin"
SCRIPT_FILE="$INSTALL_DIR/network_monitor.sh"
SERVICE_FILE="/etc/systemd/system/network-monitor.service"
SCRIPT_URL="https://gitee.com/xiaotanya/cipan/raw/master/network_monitor.sh"

echo "========================================"
echo " 网络监控脚本安装/更新工具"
echo "========================================"
echo "1) 全新安装（重新配置参数）"
echo "2) 更新脚本（保留历史配置）"
echo "========================================"
read -p "请选择模式 [1/2]: " MODE

if [ "$MODE" = "1" ]; then
    # === 模式 1：全新安装 ===
    echo "执行全新安装..."

    read -p "请输入设备名称 [默认: 主服务器]: " DEVICE_NAME
    DEVICE_NAME=${DEVICE_NAME:-"主服务器"}

    read -p "请输入网络设备名称 [默认: m-ms-main]: " NET_DEVICE
    NET_DEVICE=${NET_DEVICE:-"m-ms-main"}

    read -p "请输入最大上传速度(Mbps) [默认: 40]: " MAX_UPLOAD_SPEED
    MAX_UPLOAD_SPEED=${MAX_UPLOAD_SPEED:-40}

    read -p "请输入阈值百分比(0-100) [默认: 80]: " THRESHOLD_PERCENT
    THRESHOLD_PERCENT=${THRESHOLD_PERCENT:-80}

    read -p "请输入Server酱SendKey(可选): " SERVERCHAN_KEY

    echo "下载最新脚本..."
    curl -s -o "$SCRIPT_FILE" "$SCRIPT_URL" || { echo "下载失败"; exit 1; }

    sed -i "s/^DEVICE_NAME=.*/DEVICE_NAME=\"$DEVICE_NAME\"/" "$SCRIPT_FILE"
    sed -i "s/^NET_DEVICE=.*/NET_DEVICE=\"$NET_DEVICE\"/" "$SCRIPT_FILE"
    sed -i "s/^MAX_UPLOAD_SPEED=.*/MAX_UPLOAD_SPEED=\"$MAX_UPLOAD_SPEED\"/" "$SCRIPT_FILE"
    sed -i "s/^THRESHOLD_PERCENT=.*/THRESHOLD_PERCENT=\"$THRESHOLD_PERCENT\"/" "$SCRIPT_FILE"
    sed -i "s/^SERVERCHAN_KEY=.*/SERVERCHAN_KEY=\"$SERVERCHAN_KEY\"/" "$SCRIPT_FILE"

    chmod +x "$SCRIPT_FILE"

    # 创建 systemd 服务
    cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=Network Monitor Service
After=network.target

[Service]
Type=simple
ExecStart=$SCRIPT_FILE
Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable network-monitor.service
    systemctl restart network-monitor.service

    echo "✅ 全新安装完成！"

elif [ "$MODE" = "2" ]; then
    # === 模式 2：更新并保留历史配置 ===
    echo "执行更新（保留历史配置）..."

    if [ ! -f "$SCRIPT_FILE" ]; then
        echo "未检测到旧脚本，请先执行模式 1 安装"
        exit 1
    fi

    # 备份旧脚本
    cp "$SCRIPT_FILE" "$SCRIPT_FILE.bak"

    # 从旧脚本提取配置
    old_device_name=$(grep '^DEVICE_NAME=' "$SCRIPT_FILE.bak" | cut -d'"' -f2)
    old_net_device=$(grep '^NET_DEVICE=' "$SCRIPT_FILE.bak" | cut -d'"' -f2)
    old_max_upload=$(grep '^MAX_UPLOAD_SPEED=' "$SCRIPT_FILE.bak" | cut -d'"' -f2)
    old_threshold_percent=$(grep '^THRESHOLD_PERCENT=' "$SCRIPT_FILE.bak" | cut -d'"' -f2)
    old_serverchan_key=$(grep '^SERVERCHAN_KEY=' "$SCRIPT_FILE.bak" | cut -d'"' -f2)

    echo "下载最新脚本..."
    curl -s -o "$SCRIPT_FILE" "$SCRIPT_URL" || { echo "下载失败"; exit 1; }

    # 写回旧配置
    sed -i "s/^DEVICE_NAME=.*/DEVICE_NAME=\"$old_device_name\"/" "$SCRIPT_FILE"
    sed -i "s/^NET_DEVICE=.*/NET_DEVICE=\"$old_net_device\"/" "$SCRIPT_FILE"
    sed -i "s/^MAX_UPLOAD_SPEED=.*/MAX_UPLOAD_SPEED=\"$old_max_upload\"/" "$SCRIPT_FILE"
    sed -i "s/^THRESHOLD_PERCENT=.*/THRESHOLD_PERCENT=\"$old_threshold_percent\"/" "$SCRIPT_FILE"
    sed -i "s/^SERVERCHAN_KEY=.*/SERVERCHAN_KEY=\"$old_serverchan_key\"/" "$SCRIPT_FILE"

    chmod +x "$SCRIPT_FILE"

    systemctl restart network-monitor.service
    echo "✅ 更新完成，历史配置已保留！"
else
    echo "输入无效，退出"
    exit 1
fi
